import os
from pathlib import Path
from dotenv import load_dotenv


loaded_env_path = None
loaded_env_path = None
try:
    app_dir = Path(__file__).resolve().parent
    env_dir = app_dir / "env"
    dotenv_file_env = os.getenv("DOTENV_FILE", "").strip()
    env_name = (os.getenv("APP_ENV") or os.getenv("ENV") or os.getenv("FAMES_ENV") or "").strip().lower()
    if dotenv_file_env:
        fp = Path(dotenv_file_env)
        if not fp.is_absolute():
            fp = (env_dir if (env_dir / dotenv_file_env).exists() else app_dir) / dotenv_file_env
        if fp.exists():
            load_dotenv(fp)
            loaded_env_path = str(fp)
        elif (env_dir / ".env").exists():
            p = env_dir / ".env"
            load_dotenv(p)
            loaded_env_path = str(p)
        elif (app_dir / ".env").exists():
            p = app_dir / ".env"
            load_dotenv(p)
            loaded_env_path = str(p)
        else:
            load_dotenv()
            loaded_env_path = None
    else:
        fname = ".env"
        if env_name in ("production", "prod"):
            fname = ".env.production"
        elif env_name in ("test", "testing", "ci"):
            fname = ".env.test"
        elif env_name in ("staging", "stage"):
            fname = ".env.staging"
        elif env_name in ("dev", "development", "local"):
            fname = ".env"
        fp_env = env_dir / fname
        fp_app = app_dir / fname
        if fp_env.exists():
            load_dotenv(fp_env)
            loaded_env_path = str(fp_env)
        elif fp_app.exists():
            load_dotenv(fp_app)
            loaded_env_path = str(fp_app)
        elif (env_dir / ".env").exists():
            p = env_dir / ".env"
            load_dotenv(p)
            loaded_env_path = str(p)
        elif (app_dir / ".env").exists():
            p = app_dir / ".env"
            load_dotenv(p)
            loaded_env_path = str(p)
        else:
            load_dotenv()
            loaded_env_path = None
except Exception:
    loaded_env_path = None

print(f"ENV file loaded: {loaded_env_path or '<none>'}")


class Settings:
    POSTGRES_DSN: str = os.getenv("POSTGRES_DSN", "postgresql+asyncpg://postgres:postgres@localhost:5432/fames")
    MONGO_DSN: str = os.getenv("MONGO_DSN", "mongodb://localhost:27017")
    MONGO_DB_NAME: str = os.getenv("MONGO_DB_NAME", "fames")

    REPORT_TEMPLATE_VERSION_CODE: str = os.getenv("REPORT_TEMPLATE_VERSION_CODE", "2024-hubei-report-1202")
    REPORTS_STORAGE_DIR: str = os.getenv("REPORTS_STORAGE_DIR", "storage/reports")

    LLM_MODEL: str = os.getenv("LLM_MODEL", "gpt-4o-mini")
    LLM_API_KEY: str = os.getenv("LLM_API_KEY", "")
    LLM_SECRET_KEY: str = os.getenv("LLM_SECRET_KEY", "")

    PDF_FONT_PATH: str = os.getenv("PDF_FONT_PATH", "")

    # Auth / JWT
    JWT_SECRET: str = os.getenv("JWT_SECRET", "dev-secret-change-me")
    JWT_EXPIRE_MINUTES: int = int(os.getenv("JWT_EXPIRE_MINUTES", "60"))
    JWT_ALGORITHM: str = os.getenv("JWT_ALGORITHM", "HS256")

    # 网络声量平台API配置
    WEIBO_API_BASE: str = os.getenv("WEIBO_API_BASE", "")
    WEIBO_CLIENT_ID: str = os.getenv("WEIBO_CLIENT_ID", "")
    WEIBO_CLIENT_SECRET: str = os.getenv("WEIBO_CLIENT_SECRET", "")
    WEIBO_ACCESS_TOKEN: str = os.getenv("WEIBO_ACCESS_TOKEN", "")

    DOUYIN_API_BASE: str = os.getenv("DOUYIN_API_BASE", "")
    DOUYIN_APP_KEY: str = os.getenv("DOUYIN_APP_KEY", "")
    DOUYIN_APP_SECRET: str = os.getenv("DOUYIN_APP_SECRET", "")
    DOUYIN_ACCESS_TOKEN: str = os.getenv("DOUYIN_ACCESS_TOKEN", "")

    WECHAT_API_BASE: str = os.getenv("WECHAT_API_BASE", "")
    WECHAT_APP_ID: str = os.getenv("WECHAT_APP_ID", "")
    WECHAT_APP_SECRET: str = os.getenv("WECHAT_APP_SECRET", "")

    BAIDU_INDEX_COOKIE: str = os.getenv("BAIDU_INDEX_COOKIE", "")

    FILE_UPLOAD_BASE: str = os.getenv("FILE_UPLOAD_BASE", "http://localhost:81/dav")
    FILE_ACCESS_BASE: str = os.getenv("FILE_ACCESS_BASE", "http://localhost:81/files")
    FILE_UPLOAD_AUTH: str = os.getenv("FILE_UPLOAD_AUTH", "")
    FILE_UPLOAD_USER: str = os.getenv("FILE_UPLOAD_USER", "")
    FILE_UPLOAD_PASS: str = os.getenv("FILE_UPLOAD_PASS", "")

    # Spider UA & Cookies
    FAMES_SPIDER_UA: str = os.getenv("FAMES_SPIDER_UA", "")
    FAMES_SPIDER_WEIBO_COOKIE: str = os.getenv("FAMES_SPIDER_WEIBO_COOKIE", "")
    FAMES_SPIDER_DOUYIN_COOKIE: str = os.getenv("FAMES_SPIDER_DOUYIN_COOKIE", "")
    FAMES_SPIDER_KUAISHOU_COOKIE: str = os.getenv("FAMES_SPIDER_KUAISHOU_COOKIE", "")
    FAMES_SPIDER_XHS_COOKIE: str = os.getenv("FAMES_SPIDER_XHS_COOKIE", "")

    @property
    def reports_dir(self) -> Path:
        p = Path(self.REPORTS_STORAGE_DIR)
        p.mkdir(parents=True, exist_ok=True)
        return p


settings = Settings()
